package com.gqb.deleteFileService;

import java.io.File;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class DeleteFileService {
	private final BlockingQueue<String> fileQueue=new LinkedBlockingQueue<String>();
	
	private static final DeleteFileService deleteFileService=new DeleteFileService();
	private  DeleteFileThread deleteFileThread=new DeleteFileThread();
	public static DeleteFileService getInstance(){
		return deleteFileService;
	}
	
	class DeleteFileThread extends Thread{
		@Override
		public void run() {
			// TODO Auto-generated method stub
			while(true){
				try {
					Thread.sleep(500);
					String filePathString=fileQueue.take();
					File file=new File(filePathString);
					file.delete();
					file.getParentFile().delete();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public void deleteFile(String path) throws InterruptedException{
		if(!deleteFileThread.isAlive())
			deleteFileThread.start();
		fileQueue.put(path);
	}
}
